package com.shangxia.leetcode.hoot100.easy;

import com.alibaba.fastjson.JSONObject;
import com.shangxia.leetcode.hoot100.easy.bean.ListNode;
import com.shangxia.leetcode.hoot100.easy.bean.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: leetcode
 * @ClassName: Solution1
 * @Time: 2020/11/21
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class Solution3 {

    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(moveZeroes(new int[]{2, 1, 0, 2, 3})));
    }

    /**
     * 283. 移动零
     * @param nums
     * @return
     * @Description: 双指针法
     */
    public static int[] moveZeroes(int[] nums) {
        int n = nums.length, left = 0, right = 0;
        while (right < n) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
        return nums;
    }

    public static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

}
